<?php

class SigGenerator {
	
	var $sig; // the sig itself
	var $colors;
	var $white;
	var $statsline = 1;
	var $font;

	/* constructor */
	function SigGenerator() {
		
	}

	function createErrorSig($text) {
		$this->overlayImage('assets/CurllSigBg1.jpg');
		$this->avatarImage('cry.gif', 1, 1);
		$this->write(90, 25, $text, 'yellow');
		$this->output();
		exit;
	}

	/* create all the colours we need for the text */
	function createColors() {
		$this->colors = array();
		$this->colors['white'] = imagecolorallocate($this->sig, 255, 255, 255);
		$this->colors['black'] = imagecolorallocate($this->sig, 0, 0, 0);
		$this->colors['yellow'] = imagecolorallocate($this->sig, 255, 216, 0);
		$this->colors['red'] = imagecolorallocate($this->sig, 255, 0, 0);
		$this->colors['green'] = imagecolorallocate($this->sig, 126, 255, 0);
	}

	/* overlays the background onto our sig (ideally overlays on top of whatever is there */
	function overlayImage($file) {
		$this->sig = imagecreatefromjpeg($file);
		$this->createColors();
	}
	
	/* checks the image cache to see if this file exists, checks timestamp, too
		returns 0 on success, 1 on failure */
	function imageCache($region, $realm, $name, $style, $hash) {
		$cache = 'cache/'.$region.'/'.$realm.'/'.$name.'_'.$style.'_'.$hash.'.jpg';
		if (!file_exists($cache)) { // no cache?
			return 1;
		}
		// check timestamp
		$mtime = filemtime($cache);
		if ($mtime < (time()-(2*60*60))) { // older than 2hrs?
			return 1;
		}
		// finally, open jpeg as if we're done!
		$image = file_get_contents($cache);
		header("Content-Type: image/jpeg");
		// cache control links:
		// http://www.mnot.net/cache_docs/#CACHE-CONTROL
		header("Last-Modified: " . gmdate("D, d M Y H:i:s", $mtime) . " GMT");
		echo $image;
		return 0;
	}
	
	/* writes the current image to the cache */
	function writeCache($region, $realm, $name, $style, $hash) {
		$cache = 'cache/'.$region.'/'.$realm.'/'.$name.'_'.$style.'_'.$hash.'.jpg';
		$cachedir = 'cache/'.$realm;
		// make sure realm dir exists
  	if (!file_exists($cachedir)) {
  		mkdir($cachedir, 0777); // full write access
  	}
  	imagejpeg($this->sig, $cache);
	}

	function avatarImage($file, $x, $y) {
		$cache = BASE_PATH.'assets/avatars/'.$file;
		// check if it's cached
		if (!file_exists($cache)) {
			// if not, grab from armory.wow.com
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, 'http://armory.worldofwarcraft.com/images/portraits/'.$file);
			curl_setopt($ch, CURLOPT_REFERER, "http://armory.worldofwarcraft.com/");
			// cos they do browser detection! :O
			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2");
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
			$contents = curl_exec($ch);
			curl_close($ch);

			// save it to the cache
			$fp = fopen($cache, 'w');
			if (!$fp) {
				$this->createErrorSig('Failed to open avatar cache');
			}
			fwrite($fp, $contents);
			fclose($fp);
		}

		// place it on the sig
		$avatar = imagecreatefromgif($cache);
		imagecopy($this->sig, $avatar, $x, $y, 0, 0, 64, 64);
	}

	/* writes some text onto the image */
	function write($x, $y, $text, $color) {
		imagestring($this->sig, 4, $x, $y, $text, $this->colors[$color]);
	}
	
	/* sets the font for the class */
	function setFont($file) {
		$this->font = $file;
	}
	
	/* writes text with a preset font onto the image */
	function writeFont($size, $x, $y, $text, $color) {
		imagettftext($this->sig, $size, 0, $x, $y, $this->colors[$color], $this->font, $text);
	}
	
	/* writes text with a preset font onto the image, with a shadow */
	function writeFontShadow($size, $x, $y, $text, $color) {
		imagettftext($this->sig, $size, 0, $x+2, $y+2, $this->colors['black'], $this->font, $text);
		imagettftext($this->sig, $size, 0, $x, $y, $this->colors[$color], $this->font, $text);
	}
	
	/* returns the fontsize that makes the text fit into the box */
	function getFontSize($size, $x, $y, $text) {
		do {
			$bbox = imagettfbbox($size, 0, $this->font, $text);
			$size--;
		} while (($bbox[4]-$bbox[6]) > $x || ($bbox[1]-$bbox[7]) > $y);
		return $size+1;
	}
	
	/* writes a stats line onto the sig, there are up to 5 */
	function writeStats($title, $value, $valuecolor) {
		$fontsize = 9;
		$y = 0;
		$line = $this->statsline;
		if ($line >= 1 && $line <= 5) {
			$y = 12*$line+2;
		} else return;
		// get the bounding box of value
		$bbox = imagettfbbox($fontsize, 0, 'assets/fonts/arialbd.ttf', $value);
		$width = $bbox[4]-$bbox[6];
		
		// write the values onto sig
		imagettftext($this->sig, $fontsize, 0, 230, $y, $this->colors['yellow'], 'assets/fonts/arialbd.ttf', $title.':');
		imagettftext($this->sig, $fontsize, 0, 395-$width, $y, $this->colors[$valuecolor], 'assets/fonts/arialbd.ttf', $value);
		
		$this->statsline++;
	}

	/* this function is to create and output the sig in the class */
	function output() {
		// output image
		header("Content-Type: image/jpeg");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		imagejpeg($this->sig);
	}
}

?>